import { toHex } from '@metamask/controller-utils';
import { withFixtures, unlockWallet } from '../../../helpers';
import { SMART_CONTRACTS } from '../../../seeder/smart-contracts';
import FixtureBuilder from '../../../fixtures/fixture-builder';
import { Driver } from '../../../webdriver/driver';
import Homepage from '../../../page-objects/pages/home/homepage';
import NftListPage from '../../../page-objects/pages/home/nft-list';
import NetworkManager, {
  NetworkId,
} from '../../../page-objects/pages/network-manager';

describe('View NFT details', function () {
  const smartContract = SMART_CONTRACTS.NFTS;

  it('user should be able to view ERC721 NFT details', async function () {
    await withFixtures(
      {
        dappOptions: { numberOfTestDapps: 1 },
        fixtures: new FixtureBuilder()
          .withNftController({
            allNftContracts: {
              '0x5cfe73b6021e818b776b421b1c4db2474086a7e1': {
                [toHex(59144)]: [
                  {
                    address: `__FIXTURE_SUBSTITUTION__CONTRACT${SMART_CONTRACTS.NFTS}`,
                    name: 'TestDappNFTs',
                    symbol: 'TDC',
                  },
                ],
                [toHex(1)]: [
                  {
                    address: `__FIXTURE_SUBSTITUTION__CONTRACT${SMART_CONTRACTS.NFTS}`,
                    name: 'TestDappNFTs',
                    symbol: 'TDC',
                  },
                ],
              },
            },
            allNfts: {
              '0x5cfe73b6021e818b776b421b1c4db2474086a7e1': {
                [toHex(59144)]: [
                  {
                    address: `__FIXTURE_SUBSTITUTION__CONTRACT${SMART_CONTRACTS.NFTS}`,
                    description: 'Test Dapp NFTs for testing.',
                    favorite: false,
                    image:
                      '',
                    isCurrentlyOwned: true,
                    name: 'Test Dapp NFTs #1',
                    standard: 'ERC721',
                    tokenId: '1',
                    chainId: 59144,
                  },
                ],
                [toHex(1)]: [
                  {
                    address: `__FIXTURE_SUBSTITUTION__CONTRACT${SMART_CONTRACTS.NFTS}`,
                    description: 'Test Dapp NFTs for testing.',
                    favorite: false,
                    image:
                      '',
                    isCurrentlyOwned: true,
                    name: 'Test Dapp NFTs #1 on mainnet',
                    standard: 'ERC721',
                    tokenId: '1',
                    chainId: 1,
                  },
                  {
                    address: `__FIXTURE_SUBSTITUTION__CONTRACT${SMART_CONTRACTS.NFTS}`,
                    description: 'Test Dapp NFTs for testing.',
                    favorite: false,
                    image:
                      '',
                    isCurrentlyOwned: true,
                    name: 'Test Dapp NFTs #2 on mainnet',
                    standard: 'ERC721',
                    tokenId: '2',
                    chainId: 1,
                  },
                ],
              },
            },
            ignoredNfts: [],
          })
          .withNetworkControllerOnMainnet()
          .withEnabledNetworks({
            eip155: {
              '0x1': true,
              '0xe708': true,
            },
          })
          .build(),
        smartContract,
        title: this.test?.fullTitle(),
      },
      async ({ driver }: { driver: Driver }) => {
        await unlockWallet(driver);

        const networkManager = new NetworkManager(driver);

        // Click to open the NFT details page and check title
        const homePage = new Homepage(driver);
        await homePage.goToNftTab();

        // Show Ethereum NFTs
        const nftListPage = new NftListPage(driver);
        await networkManager.openNetworkManager();
        await networkManager.selectNetworkByChainId(NetworkId.ETHEREUM);
        await nftListPage.checkNumberOfNftsDisplayed(2);

        await nftListPage.checkNftNameIsDisplayed(
          'Test Dapp NFTs #1 on mainnet',
        );
        await nftListPage.checkNftNameIsDisplayed(
          'Test Dapp NFTs #2 on mainnet',
        );

        // Show All NFTs
        await networkManager.openNetworkManager();
        await networkManager.selectAllNetworks();

        await nftListPage.checkNumberOfNftsDisplayed(3);

        await nftListPage.checkNftNameIsDisplayed('Test Dapp NFTs #1');

        await nftListPage.checkNftNameIsDisplayed(
          'Test Dapp NFTs #1 on mainnet',
        );
        await nftListPage.checkNftNameIsDisplayed(
          'Test Dapp NFTs #2 on mainnet',
        );
      },
    );
  });
});
